package org.ly.uap.client.authentication;

import java.util.Collections;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ly.uap.client.proxy.ProxyRetriever;
import org.ly.uap.client.util.CommonUtils;

public class AttributePrincipalImpl extends SimplePrincipal
        implements AttributePrincipal {
    private static final Log LOG = LogFactory.getLog(AttributePrincipalImpl.class);
    private static final long serialVersionUID = -1443182634624927187L;
    private final Map<String, Object> attributes;
    private final String proxyGrantingTicket;
    private final ProxyRetriever proxyRetriever;

    public AttributePrincipalImpl(String name) {
        this(name, Collections.emptyMap());
    }

    public AttributePrincipalImpl(String name, Map<String, Object> attributes) {
        this(name, attributes, null, null);
    }

    public AttributePrincipalImpl(String name, String proxyGrantingTicket, ProxyRetriever proxyRetriever) {
        this(name, Collections.emptyMap(), proxyGrantingTicket, proxyRetriever);
    }

    public AttributePrincipalImpl(String name, Map<String, Object> attributes, String proxyGrantingTicket, ProxyRetriever proxyRetriever) {
        super(name);
        this.attributes = attributes;
        this.proxyGrantingTicket = proxyGrantingTicket;
        this.proxyRetriever = proxyRetriever;

        CommonUtils.assertNotNull(this.attributes, "attributes cannot be null.");
    }

    public Map<String, Object> getAttributes() {
        return this.attributes;
    }

    public String getProxyTicketFor(String service) {
        if (this.proxyGrantingTicket != null) {
            return this.proxyRetriever.getProxyTicketIdFor(this.proxyGrantingTicket, service);
        }

        LOG.debug("No ProxyGrantingTicket was supplied, so no Proxy Ticket can be retrieved.");
        return null;
    }
}
